<%@ page import="POJO.*" %>
<%@ page import="DB.*" %>

<%
            boolean ok = true;
            UserManager db = new UserManager(DBInfo.url, DBInfo.username, DBInfo.password);
            TripManager db2 = new TripManager(db.conn);
            ContentContainerManager db3 = new ContentContainerManager(db.conn);
            RelationManager db4 = new RelationManager(db.conn);
            User u = (User) session.getAttribute("user");
            u.GetInfo();
            if (request.getParameter("page") == null || !request.getParameter("page").equals("trip") || request.getParameter("tripid") == null || u == null) {
                ok = false;
%>

<h2 class="nice">Unauthorized entrance.</h2>

<%  } else if (!db.isConnected() || !db2.isConnected()) {
    ok = false;
    out.print("<h2 class=\"nice\">No connection with the database.</h2>");
} else {
    int tripId = Integer.parseInt(request.getParameter("tripid"));
    String username = u.username;
    String[] owners = db2.getOwners(tripId);
    String rel = "OTHER";
    if (username != null) {
        rel = db4.getRelationWithGroup(owners, username);
    }

    if (username != null && request.getMethod().equals("POST") && request.getParameter("requestOwnership") != null && request.getParameter("requestOwnership").compareTo("true") == 0) //nieuwe waarden zijn verstuurd
    {
        db2.makeOwnershipRequest(tripId, username);
    }

    out.print("<h1 class=\"nice\">" + db2.getTripName(tripId) + "</h1>");
    out.print("<div style=\"text-align:left;\">");
    out.print("<table class=\"nice\" width = \"100%\" style=\"text-align:left;\">");
    out.print("<tr><td class=\"b\">Start Date:</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + db2.getStart(tripId) + "</td></tr>");
    out.print("<tr><td class=\"b\">End Date:</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + db2.getEnd(tripId) + "</td></tr>");
    out.print("</table></div>");

    int[] containers = null;
    if (rel.compareTo("OTHER") == 0) {
        containers = db3.getAllContentContainersOfTripForEverybody(tripId);
    } else if (rel.compareTo("FRIEND") == 0) {
        containers = db3.getAllContentContainersOfTripForFriends(tripId);
    } else if (rel.compareTo("FAMILY") == 0) {
        containers = db3.getAllContentContainersOfTripForFamily(tripId);
    }



    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Owners:</td></tr>");
    for (int i = 0; owners != null && i < owners.length; ++i) {
        out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=profile&amp;user=" + owners[i] + "\">" + owners[i] + "</a></td></tr>");
    }
    out.print("</table></div>");

    out.print("<div><table width=\"100%\" class=\"nice\" style=\"text-align:left;\"><tr><td colspan=\"2\" class=\"b\">Stops:</td></tr>");
    /*for (int i = 0; containers != null && i < containers.length; ++i) {
    out.print("<tr><td style=\"text-align:left;\">&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"nice\"  href=\"index.jsp?page=triplocation&amp;containerid=" + containers[i] + "\">Stop nr. " + (containers.length - i) + " (" + db3.getArrival(containers[i]) + ")</a></td></tr>");
    }*/

    Maps map = new Maps();
    out.print("<tr><td><div  id=\"map_canvas\" style=\"height: 300px; width:400px;\"></div></td></tr>");
    out.print("</table></div>");
    if (username != null) {
        if (db2.isDeclinedOwnerOf(tripId, username)) {
            out.print("<div class=\"nice\"><div class=\"b\">Your ownership request has been rejected.</div></div>");
        } else if (db2.isUnprocessedOwnerOf(tripId, username)) {
            out.print("<div class=\"nice\"><div class=\"b\">You requested ownership.</div></div>");
        } else if (db2.isAccepted(tripId, username)) {
            out.print("<div class=\"nice\"><div class=\"b\">You are an owner.</div></div>");
        } else {
            out.print("<div class=\"nice\"><form action=\"index.jsp?page=trip&amp;tripid=" + tripId + "&amp;requestOwnership=true\" method=\"post\"><div class=\"b\">" +
                    "<div style=\"text-align:center; width:100%;\"><input type=\"submit\" value=\"Request ownership\" /></div></div></form></div>");
        }
    }

    out.print(map.tripMap(tripId, (User) session.getAttribute("user"), false));
}
%>
